1775D - Friendly Spiders - CodeForces Solution


dfs and similar graphs math number theory shortest paths *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(i, n) for (ll i = 0; i < n; i++)
#define forn(i, n) for (ll i = 1; i < n; i++)
#define forne(i, n) for (ll i = 1; i <= n; i++)
#define revn(i, n) for (ll i = n - 1; i >= 0; i--)
#define sortl(v) sort(v.begin(), v.end())
#define reversal(v) reverse(v.begin(), v.end())
#define countset(n) __builtin_popcountll(n)
#define maxelement(v) *max_element(v.begin(), v.end())
#define minelement(v) *min_element(v.begin(), v.end())
#define vi vector<ll>
#define f(map) for (auto it : map)
#define read(n) \
    ll n;       \
    cin >> n;
const int inf = 1e9 + 7;
const int maxn = 3e5 + 10;
const int M = 2 * maxn;
vector<ll> fact(1e6);
vector<ll> inversion(1e6);
vector<bool> seive(maxn, true);
vector<ll> primes(maxn, 0);
void sieveness()
{
    for (int i = 2; i < maxn; i++)
        if (!primes[i])
            for (int j = i; j < maxn; j += i)
                primes[j] = i;
}
vector<ll> par(M, -1);
vector<ll> e[M];
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    sieveness();
    ll test = 1;
    // cin >> test;
    while (test--)
    {

        ll i, j, a, b, c, d, v1, v2, f, cnt, ans, print;
        read(n);
        forne(i, n)
        {
            cin >> a;
            // cout << a << endl;
            while (a > 1)
            {
                e[i].push_back(maxn + primes[a]);
                e[primes[a] + maxn].push_back(i);
                a /= primes[a];
            }
        }
        queue<int> q;
        read(s);
        read(t);
        q.push(s);
        par[s] = s;
        while (q.size())
        {
            ll u = q.front();
            q.pop();
            for (auto it : e[u])
            {
                if (par[it] != -1)
                    continue;
                par[it] = u;
                q.push(it);
            }
        }
        if (par[t] == -1)
        {
            cout << "-1" << endl;
            continue;
        }
        vector<ll> res;
        res.push_back(t);
        while (par[t] != s)
        {
            t = par[par[t]];
            res.push_back(t);
        }
        reversal(res);
        cout << res.size() << endl;
        for (auto it : res)
        {
            cout << it << " ";
        }
        cout << endl;
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number